package Exercise;

import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise01 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();
    }
}

class A extends Thread{
    private boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        Random random = new Random();
        while (flag){
            int i = random.nextInt(100 + 1);
            System.out.println(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
}

class B extends Thread{
    private A a;

    public B(A a){
    this.a = a;
    }

    private Scanner scanner = new Scanner(System.in);

    @Override
    public void run() {
        while (true){
            System.out.println("请输入(Q退出):");
            if(scanner.next().toUpperCase(Locale.ROOT).charAt(0) == 'Q'){
            a.setFlag(false);
            System.out.println("退出线程..");
            break;
            }
        }
    }
}
